package java_thinking.unit_21._21_7;

import java.math.BigInteger;
import java.util.concurrent.BlockingQueue;

/**
 * @Desc:   7-5 通过中断来取消
 * @author: cww
 * @DateTime: 2019/6/13 20:40
 */

public class PrimeProducer extends Thread{
    private final BlockingQueue<BigInteger> queue;

    public PrimeProducer(BlockingQueue<BigInteger> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        try {
            BigInteger p = BigInteger.ONE;
            while (!Thread.currentThread().isInterrupted()) {
                queue.put(p.nextProbablePrime());
            }
        } catch (InterruptedException e) {
            /* 允许线程退出，这里屏蔽了中断，但是因为它已经知道线程将要结束，
            * 因此在调用栈中已经没有上层代码需要知道中断信息*/
        }
    }
    public void cancel() {
        //这样就可以对当前线程发出中断请求了
        interrupt();
    }
}
